//
//  PListManager.m
//  SpeedTap
//
//  Created by Chi Ho Chiu on 2/9/11.
//  Copyright 2011 Individual. All rights reserved.
//

#import "PListManager.h"


@implementation PListManager
@synthesize parentDict;

-(id) init
{
    if( (self = [super init]) )
    {
        
    }
    
    return self;
}

-(void) dealloc
{
    [parentDict release];
    [super dealloc];
}

+(PListManager *) openFile:(NSString *)strFileName
{
    PListManager *plistManager = [PListManager node];
    
	NSString* pListPath = [[NSBundle mainBundle] pathForResource:strFileName ofType:@"plist"];
	plistManager.parentDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
        
    return plistManager;
}

-(void) closeFile
{
    //Do nothing
    //No need to call [super dealloc] because, this is autorelease object
}

-(id) getValueByPath:(CCArray *)pathArray key:(NSString *)strKey
{
    CCArray *leafArray = [self iteratePathArray:pathArray];
    
    //If no parent 
    if(leafArray == nil)
    {
        return [parentDict valueForKey:strKey]; //Get the value from the top
    }
    else
    {
        //Return the key
        return [leafArray valueForKey:strKey];
    }
}

-(NSMutableDictionary *) getSomeValuesByPath:(CCArray *)pathArray keys:(CCArray *)strKeys
{
    CCArray *leafArray = [self iteratePathArray:pathArray];
    
    NSMutableDictionary *returnDict = [NSMutableDictionary dictionaryWithCapacity:[strKeys count]];
        
    //No Parent Node
    if(leafArray == nil)
    {
        for(NSString *strKey in strKeys)
        {
            id value = [parentDict valueForKey:strKey];
            if(value != nil)
                [returnDict setObject:value forKey:strKey];
        }
    }
    else
    {
        for(NSString *strKey in strKeys)
        {
            id value = [leafArray valueForKey:strKey];
            if(value != nil)
                [returnDict setObject:value forKey:strKey];
        }
    }

    return returnDict;
}

-(NSMutableDictionary *) getAllValuesByPath:(CCArray *)pathArray
{
    CCArray *leafArray = [self iteratePathArray:pathArray];
    NSMutableDictionary *returnDict = [NSMutableDictionary dictionaryWithCapacity:[leafArray count]];
    
    //No Parent Node
    if(leafArray == nil)
    {
        for(id node in parentDict)
        {
            id value = [parentDict valueForKey:node];
            if(value != nil)
                [returnDict setObject:value forKey:node];
        }
    }
    else
    {
        for(id node in leafArray)
        {
            id value = [leafArray valueForKey:node];
            if(value != nil)
                [returnDict setObject:value forKey:node];
        }
    }
    
    return returnDict;
}

-(CCArray *)iteratePathArray:(CCArray *)pathArray
{
    //No parent Node
    if(pathArray == nil)
    {
        return nil;
    }
    
    CCArray *currentPathArray = nil;
    
    //Get the first Sub Array
    NSString *strSubPath = (NSString *)[pathArray objectAtIndex:0];
    currentPathArray = [parentDict valueForKey:strSubPath];
    
    //Path is invalid
    if(currentPathArray == nil)
        return nil;
    
    //Get the following Sub Array
    for(int i=1; i<[pathArray count]; i++)
    {
        strSubPath = (NSString *)[pathArray objectAtIndex:i];
        currentPathArray = [currentPathArray valueForKey:strSubPath];
        
        if(currentPathArray == nil) //Path is invalid
            return nil;
    }
    
    return currentPathArray;
}
@end
